<template>
  <a-layout-header class="header">
    <div class="logo"/>
    <a class="login-menu" v-show="!user.id" @click="showLoginModel">
      <span>Login</span>
    </a>
    <a-popconfirm
        title="确认退出登录?"
        ok-text="是"
        cancel-text="否"
        @confirm="logout"
    >
      <a class="login-menu" v-show="user.id">
        <span>退出登录</span>
      </a>
    </a-popconfirm>
    <a class="login-menu" v-show="user.id">
      <span>您好:{{user.name}}</span>
    </a>
    <a-menu
        v-model:selectedKeys="selectedKeys1"
        theme="dark"
        mode="horizontal"
        :style="{ lineHeight: '64px' }"
    >
      <a-menu-item key="home">
        <router-link to="/">Home</router-link>
      </a-menu-item>
      <a-menu-item key="aboutMe">
        <router-link to="/about">About me</router-link>
      </a-menu-item>
      <a-menu-item key="AdminUser" :style="user.id? null : {display: 'none'}"> <!--user.id有值的话什么都不做，自己会显示-->
        <router-link to="/admin/user">UserManage</router-link>
      </a-menu-item>

      <a-menu-item key="AdminEbook" :style="user.id? null : {display: 'none'}">
        <router-link to="/admin/ebook">EbookManage</router-link>
      </a-menu-item>

      <a-menu-item key="AdminCategory" :style="user.id? null : {display: 'none'}">
        <router-link to="/admin/category">CategoryManage</router-link>
      </a-menu-item>



    </a-menu>
    <a-modal
    title="登录"
    v-model:visible="loginModelVisible"
    :confirm-loading="loginModelLoading"
    @ok="login"
    >
      <a-form :model="loginUser" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
        <a-form-item label="登录名">
          <a-input v-model:value="loginUser.loginName"/>
        </a-form-item>
        <a-form-item label="密码">
          <a-input v-model:value="loginUser.password" type="password"/>
        </a-form-item>
      </a-form>
    </a-modal>
  </a-layout-header>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue';
import axios from 'axios';
import { message } from 'ant-design-vue';
import store from "@/store";

declare let hexMd5: any;
declare let KEY: any;

export default defineComponent({
  name: 'the-header',
  setup () {
    // 登录后保存
    const user = computed(() => store.state.user);

    // 用来登录
    const loginUser = ref({
      loginName: "test1",
      password: "bbb123456"
    });
    const loginModelVisible = ref(false);
    const loginModelLoading = ref(false);
    const showLoginModel = () => {
      loginModelVisible.value = true;
    };

    // 登录
    const login = () => {
      console.log("开始登录");
      loginModelLoading.value = true;
      loginUser.value.password = hexMd5(loginUser.value.password + KEY);
      axios.post('/user/login', loginUser.value).then((response) => {
        loginModelLoading.value = false;
        const data = response.data;
        if (data.success) {
          loginModelVisible.value = false;
          message.success("登录成功！");
          // user.value = data.content;
          store.commit("setUser", data.content);
        } else {
          message.error(data.message);
        }
      });
    };

    // 退出登录
    const logout = () => {
      console.log("退出登录开始");
      axios.get('/user/logout/' + user.value.token).then((response) => {
        const data = response.data;
        if (data.success) {
          message.success("退出登录成功！");
          store.commit("setUser", {});
        } else {
          message.error(data.message);
        }
      });
    };

    return {
      loginModelVisible,
      loginModelLoading,
      showLoginModel,
      loginUser,
      login,
      user,
      logout,
    }
  }
});
</script>

<style>
 .login-menu {
  float: right;
  color: white;
   padding-left: 10px;
 }
</style>